﻿package PinballCode {
	
	public class Plunger extends Rectangle{
		
		//Attrtibutes
		private var k:Number = 40; 		//spring constance
		private var startHeight:int;	// startHeight of the plunger
		private var compressRate:int = 2;	// rate at which the plunger compresses
		
		private var vy:Number = 0;		// velocity in the y
		private var friction:Number = .9;
		
		private var minHeight:Number;
		
		public function Plunger(ht:int,widt:int) {
			// constructor code
			super(-ht,widt);
			startHeight = this.ht;
			minHeight = startHeight - startHeight/3;
		}
		
		private function disp():Number {
			return startHeight - ht;
		}
		
		// when the space bar is held
		public function hold(dt:Number):void {
			ht += compressRate * dt;
			if(ht > minHeight)
				ht = minHeight;
			init();
		}
		
		// when the space bar is released
		public function release():Number {
			return disp() * k * friction;
		}
		
		
		// the plunger is trying to go back to its original state
		public function reset(dt:Number):void {
			this.vy = disp() * k * (.04) * friction;
			ht += vy;
			init();
				
		}

	}
	
}
